package com.greatwall.hip.cms.controller;

import cn.hutool.core.date.*;
import cn.hutool.core.util.NumberUtil;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.greatwall.hip.cms.constant.Constant;
import com.greatwall.hip.cms.model.Req;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TianLei
 * @version V1.0
 */
public class BaseController {

    protected void checkDateReq(Req req) {
        Preconditions.checkNotNull(req, "请求参数不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getStartDate()), "开始时间不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getEndDate()), "结束时间不能为空");
    }

    protected void checkAllReq(Req req) {
        Preconditions.checkNotNull(req, "请求参数不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getType()), "统计类型不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getStartDate()), "开始时间不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getEndDate()), "结束时间不能为空");
        if(StringUtils.equals(Constant.YEAR,req.getType())) {
            Preconditions.checkArgument(req.getStartDate().length() == 4 && req.getEndDate().length() == 4, "时间格式不对");
        } else if (StringUtils.equals(Constant.MONTH,req.getType())) {
            Preconditions.checkArgument(req.getStartDate().length() == 6 && req.getEndDate().length() == 6, "时间格式不对");
        } else {
            Preconditions.checkArgument(req.getStartDate().length() == 8 && req.getEndDate().length() == 8, "时间格式不对");
        }
    }

    protected String divScale(Number v1, Number v2, int scale) {
        if(v1 == null || v2 == null || v2.intValue() == 0) {
            return "0";
        }
        BigDecimal decimal = NumberUtil.div(v1, v2);
        return NumberUtil.roundStr(decimal.doubleValue() * 100, scale);
    }

    protected List<String> getNearYearMonthDate(){
        ArrayList<String> list = Lists.newArrayList();

        DateRange range = DateUtil.range(DateUtil.offset(DateTime.now(), DateField.MONTH, -11), DateTime.now(), DateField.MONTH);
        range.forEach(d -> {
            String s = d.toString(DatePattern.SIMPLE_MONTH_PATTERN);
            list.add(s);
        });
        return list;
    }

    protected List<String> rangeMonth(String startDate, String endDate){
        ArrayList<String> list = Lists.newArrayList();
        long betweenMonth = DateBetween.create(DateUtil.parse(startDate, DatePattern.SIMPLE_MONTH_PATTERN), DateUtil.parse(endDate, DatePattern.SIMPLE_MONTH_PATTERN)).betweenMonth(false);

        for (int i = 0; i <= betweenMonth; i++) {
            list.add(DateUtil.format(DateUtil.offsetMonth(DateUtil.parse(startDate, DatePattern.SIMPLE_MONTH_PATTERN), i), DatePattern.SIMPLE_MONTH_PATTERN));
        }
        return list;
    }

    protected List<String> rangeDay(String startDate, String endDate){
        ArrayList<String> list = Lists.newArrayList();
        long betweenDay = DateBetween.create(DateUtil.parse(startDate, DatePattern.PURE_DATE_PATTERN), DateUtil.parse(endDate, DatePattern.PURE_DATE_PATTERN)).between(DateUnit.DAY);

        for (int i = 0; i <= betweenDay; i++) {
            list.add(DateUtil.format(DateUtil.offsetDay(DateUtil.parse(startDate, DatePattern.PURE_DATE_PATTERN), i), DatePattern.PURE_DATE_PATTERN));
        }
        return list;
    }

    protected List<String> rangeYear(String startDate, String endDate){
        ArrayList<String> list = Lists.newArrayList();
        long betweenYear = DateBetween.create(DateUtil.parse(startDate, "yyyy"), DateUtil.parse(endDate, "yyyy")).betweenYear(false);
        for (int i = 0; i <= betweenYear; i++) {
            list.add(DateUtil.format(DateUtil.offset(DateUtil.parse(startDate, "yyyy"),DateField.YEAR, i), "yyyy"));
        }
        return list;
    }

    protected List<String> rangeDate(Req req) {
        List<String> date = null;
        switch (req.getType()) {
            case Constant.YEAR:
                date = rangeYear(req.getStartDate(), req.getEndDate());
                break;
            case Constant.MONTH:
                date = rangeMonth(req.getStartDate(), req.getEndDate());
                break;
            default:
                date = rangeDay(req.getStartDate(), req.getEndDate());
        }
        return date;
    }
}
